<?php

/**
 * This is the model class for table "prefix_blog".
 *
 * The followings are the available columns in table 'prefix_blog':
 * @property string $blog_id
 * @property string $user_owner_id
 * @property string $blog_title
 * @property string $blog_description
 * @property string $blog_type
 * @property string $blog_date_add
 * @property string $blog_date_edit
 * @property double $blog_rating
 * @property string $blog_count_vote
 * @property string $blog_count_user
 * @property double $blog_limit_rating_topic
 * @property string $blog_url
 * @property string $blog_avatar
 * @property User $owner
 * @property array(User) $users
 * @property array(Topic) $topics
 * @property array(TopicTag) $topicTags
 */
class Blog extends LsActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Blog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getEntity(){
		if($entity = parent::getEntity()){
			
			if($this->hasRelated('userBlogUser')
			&& $blogUser = $this->getRelated('userBlogUser')){
				$entity->setUserIsJoin((bool) $blogUser);
				$entity->setUserIsAdministrator($blogUser->user_role==ModuleBlog::BLOG_USER_ROLE_ADMINISTRATOR);
				$entity->setUserIsModerator($blogUser->user_role==ModuleBlog::BLOG_USER_ROLE_MODERATOR);
			}
			
			return $entity;
		}
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return Config::Get('db.table.blog');
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_owner_id, blog_title, blog_description, blog_date_add', 'required'),
			array('blog_rating, blog_limit_rating_topic', 'numerical'),
			array('user_owner_id, blog_count_vote, blog_count_user', 'length', 'max'=>11),
			array('blog_title, blog_url', 'length', 'max'=>200),
			array('blog_type', 'length', 'max'=>8),
			array('blog_avatar', 'length', 'max'=>250),
			array('blog_date_edit', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('blog_id, user_owner_id, blog_title, blog_description, blog_type, blog_date_add, blog_date_edit, blog_rating, blog_count_vote, blog_count_user, blog_limit_rating_topic, blog_url, blog_avatar', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'owner' => array(self::BELONGS_TO, 'User', 'user_owner_id'),
			'users' => array(self::MANY_MANY, 'User', Config::Get('db.table.blog_user').'(blog_id, user_id)', 'index'=>'user_id'),
			'topics' => array(self::HAS_MANY, 'Topic', 'blog_id', 'index'=>'topic_id'),
			'countTopics' => array(self::STAT, 'Topic', 'blog_id'),
			'topicTags' => array(self::HAS_MANY, 'TopicTag', 'blog_id', 'index'=>'topic_tag_id'),
			'vote' => array(
				self::HAS_ONE,
				'Vote',
				'target_id',
				'condition' => '',
				'on' => '{%child}.target_type="blog"'
					.' and {%child}.user_voter_id='.(User::me()?User::me()->user_id:0),
			),
			'userBlogUser' => array(
				self::HAS_ONE,
				'BlogUser',
				'blog_id',
				'on' => '{%child}.user_id='.(User::me()?User::me()->user_id:0),
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'blog_id' => 'Blog',
			'user_owner_id' => 'User Owner',
			'blog_title' => 'Blog Title',
			'blog_description' => 'Blog Description',
			'blog_type' => 'Blog Type',
			'blog_date_add' => 'Blog Date Add',
			'blog_date_edit' => 'Blog Date Edit',
			'blog_rating' => 'Blog Rating',
			'blog_count_vote' => 'Blog Count Vote',
			'blog_count_user' => 'Blog Count User',
			'blog_limit_rating_topic' => 'Blog Limit Rating Topic',
			'blog_url' => 'Blog Url',
			'blog_avatar' => 'Blog Avatar',
		);
	}
	
	public function getTitleSafe(){
		return htmlspecialchars($this->blog_title);
	}
	
	public function accessible($user=null){
		if(!$user){
			$user=User::me();
		}
		$crit = $this->getDbCriteria();
		//$crit->alias = $this->tableName();
		
		$orCondition = array();
		if(!$user){
			$orCondition[] = "{%this}.blog_type in('open','personal')";
		}elseif($user && !$user->userAdministrator){
			$orCondition[] = "{%this}.blog_type in('open','personal')";
			$blogIds = Yii::E()->Blog_GetAccessibleBlogsByUser($user->entity);
			if($blogIds){
				$orCondition[] = "{%this}.blog_id in(".join(',',$blogIds).")";
			}
		}
		if($orCondition){
			$crit->addCondition('('.join(' or ', $orCondition).')');
		}
		return $this;
	}
	
	public function scopes(){
		return array(
			'personal' => array(
				'condition' => '{%this}.blog_type="personal"',
				'alias' => $this->tableName(),
			),
			'open' => array(
				'condition' => '{%this}.blog_type="open"',
				'alias' => $this->tableName(),
			),
			'collective' => array(
				'condition' => '{%this}.blog_type!="personal"',
				'alias' => $this->tableName(),
			),
		);
	}
}